<?php
namespace app\admin\controller;

class Server extends Common
{
    public function index()
    {
        $where=[];
        $list=db("server")->alias("n")
            ->leftJoin('server_category c','c.id = n.cate_id')
            ->field("n.*,c.title as cate_title")
            ->where($where)->paginate(10);
        $cate=db("server_category")->select();
        return $this->fetch('index',compact("list","cate"));
    }
    public function edit()
    {
        if(request()->isPost()){
            $data=input();
            $insert['title']=$data['title'];
            $insert['status']=$data['status'];
            $insert['image']=$data['image'];
            if(input("id")){
                $res= db("server")->where(['id'=>$data['id']])->update($insert);
            }else{
                $res=db("server")->insert($insert);
            }
            if($res!==false){
                return $this->renderSuccess('操作成功',url('server/index'));
            }
            return $this->renderError('操作失败');
        }
        $where['id']=input("id");
        $info=db("server")
            ->where($where)
            ->find();
        $cate=db("news_category")->select();
        return $this->fetch('edit',compact("info","cate"));
    }
    public function server_del()
    {
        if(request()->isPost()){
            $id=input("id");
             $res= db("server")->where(['id'=>$id])->delete();
            if($res!==false){
                return   $this->renderSuccess('删除成功',url('server/index'));
            }
            return  $this->renderError('删除失败',url('server/index'));
        }
        return  $this->renderError('非法访问',url('server/index'));
    }

    public function category()
    {
        $list=db("server_category")->select();
        return $this->fetch('category',compact("list"));
    }
    public function cate_edit()
    {
        if(request()->isPost()){
            $data=input();
            unset($data['/admin/server/cate_edit_html']);
            if(input("id")){
                $res= db("server_category")->where(['id'=>$data['id']])->update($data);
            }else{
                $data['create_time']=time();
                $res=db("server_category")->insert($data);
            }
            if($res!==false){
                $this->success('操作成功',url('server/category'));
            }
            $this->error('操作失败');
        }
        $where['id']=input("id");
        $info=db("server_category")
            ->where($where)
            ->find();
        return $this->fetch('cate_edit',compact("info"));
    }
    public function cate_del()
    {
        if(request()->isPost()){
            $id=input("id");
            $server=db('server')->where(['cate_id'=>$id])->find();
            if($server){
                return  $this->renderError('该分类下还有新闻，不能删除',url('server/category'));
            }
            $res= db("server_category")->where(['id'=>$id])->delete();
            if($res!==false){
                return   $this->renderSuccess('删除成功',url('server/category'));
            }
            return  $this->renderError('删除失败',url('server/category'));
        }
        return  $this->renderError('非法访问',url('server/category'));
    }
}
